/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.server.action.service;

import han.bifang.business.service.ServiceManager;
import han.bifang.common.entity.SoaHttpBinding;
import han.bifang.common.entity.SoaJmsBinding;
import han.bifang.common.entity.SoaJmsBindingWrapper;
import han.bifang.common.entity.SoaMethodParam;
import han.bifang.common.entity.SoaMethodQos;
import han.bifang.common.entity.SoaMqJms;
import han.bifang.common.entity.SoaNormalJms;
import han.bifang.common.entity.SoaServiceVersion;
import han.bifang.common.entity.SoaSoapBinding;
import han.bifang.common.entity.SoaVersionQos;
import han.bifang.common.entity.SoaWebServicesBinding;
import han.bifang.common.entity.SoaWebServicesMethod;
import han.bifang.common.entity.SoaWebServicesMethodWrapper;
import han.bifang.common.entity.SoaWsdl;
import han.bifang.common.entity.SoaWsdlWrapper;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.MethodQos;
import han.bifang.gwt.client.common.entity.SoapBinding;
import han.bifang.gwt.client.common.entity.Version;
import han.bifang.gwt.client.common.entity.VersionQos;
import han.bifang.gwt.client.common.entity.WebServicesMethodWrapper;
import han.bifang.gwt.client.common.entity.WsdlWrapper;
import han.bifang.gwt.server.action.BifangGWTAction;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @Author Kent
 */
public class CreateVersionAction implements BifangGWTAction {

	
	public ResponseObject execute(RequestObject req,HttpServletRequest request,HttpServletResponse response) throws BifangException {
		ResponseObject res = new ResponseObject();
		try {
			ArrayList r = (ArrayList)req.getRequest();
			Log logger = LogFactory.getLog(CreateVersionAction.class);
			logger.debug(r);
			Version v = (Version)r.get(0);
			Object b = r.get(1);
			ArrayList qos = (ArrayList)r.get(2);
			
			SoaServiceVersion sv = new SoaServiceVersion();
			BeanUtils.copyProperties(sv, v);
			
			if(b instanceof HttpBinding){
				SoaHttpBinding hp = new SoaHttpBinding();
				BeanUtils.copyProperties(hp, b);
				b = hp;
			}else if(b instanceof SoapBinding){
				SoaSoapBinding hp = new SoaSoapBinding();
				BeanUtils.copyProperties(hp, b);
				b = hp;
			}else if(b instanceof JmsBindingWrapper){
				JmsBindingWrapper w = (JmsBindingWrapper)b;
				SoaJmsBindingWrapper sw = new SoaJmsBindingWrapper();
				SoaJmsBinding jb = new SoaJmsBinding();
				
				BeanUtils.copyProperties(jb,w.getJmsBinding());
				sw.setJmsBinding(jb);
				if(jb.getServer().equals("WebSphere MQ")){
					SoaMqJms mq = new SoaMqJms();
					BeanUtils.copyProperties(mq, w.getInfo());
					sw.setInfo(mq);
				}else{
					SoaNormalJms j = new SoaNormalJms();
					BeanUtils.copyProperties(j, w.getInfo());
					sw.setInfo(j);
				}
				b = sw;
			}else if(b instanceof WsdlWrapper){
				SoaWsdlWrapper sw =  new SoaWsdlWrapper();
				WsdlWrapper w = (WsdlWrapper)b;
				SoaWebServicesBinding wb = new SoaWebServicesBinding();
				BeanUtils.copyProperties(wb, w.getWebServicesBinding());
				sw.setWebServicesBinding(wb);
				
				SoaWsdl wsdl = new SoaWsdl();
				wsdl.setWsdl((String)request.getSession().getAttribute("wsdl"));
				sw.setWsdl(wsdl);
				
				
				ArrayList re = new ArrayList();
				for(int i = 0 , j = w.getMethods().size() ; i < j ; i ++){
					WebServicesMethodWrapper wmw = (WebServicesMethodWrapper)w.getMethods().get(i);
					SoaWebServicesMethodWrapper swmw = new SoaWebServicesMethodWrapper();
					SoaWebServicesMethod swm = new SoaWebServicesMethod();
					BeanUtils.copyProperties(swm, wmw.getMethod());
					SoaMethodParam smp = new SoaMethodParam();
					BeanUtils.copyProperties(smp, wmw.getParams());
					
					swmw.setMethod(swm);
					swmw.setParams(smp);
					ArrayList<SoaMethodQos> t = new ArrayList<SoaMethodQos>();
					for(int m = 0 , n = wmw.getQos().size(); m < n ; m ++){
						MethodQos mq = wmw.getQos().get(m);
						SoaMethodQos smq = new SoaMethodQos();
						BeanUtils.copyProperties(smq, mq);
						t.add(smq);
					}
					swmw.setQos(t);
					re.add(swmw);
					
					
				}
				sw.setMethods(re);
				b = sw;
				
			}
			ArrayList sqos = new ArrayList();
			for(int i = 0 , j = qos.size() ; i < j ; i ++){
				VersionQos q = (VersionQos)qos.get(i);
				SoaVersionQos sq = new SoaVersionQos();
				BeanUtils.copyProperties(sq, q);
				sqos.add(sq);
			}
			String id = ServiceManager.createVersion(sv, b, sqos);
			
			res.setCode("0");
			res.setResponse(id);
		} catch (BifangException e) {
			throw e;	
		}catch (Exception e) {
			throw new BifangException(BifangErrorCode.UNKNOWN_ERROR,e.getMessage(),e);		
		}
		return res;
	}

}
